package at.technikum_wien.embsys.aat.PriscillaCore.component;

import java.util.Iterator;
import java.util.List;

import at.technikum_wien.embsys.aat.PriscillaCore.enums.EnoceanComponentType;
import at.technikum_wien.embsys.aat.PriscillaCore.event.FrameEventLearnTelegram;
import at.technikum_wien.embsys.aat.PriscillaCore.link.event.LinkListener;
import at.technikum_wien.embsys.aat.PriscillaCore.telegram.Telegram;



/**
 * Message for receiving a teach-in telegram
 * 
 * This "component" will be created, if a teach-in
 * telegram is received
 * 
 * @author B. Aigner
 *
 */
public class LearnTelegram implements EnoceanComponent {

	List<?> eventListener;
	EnoceanComponentType enoceanComponentType = EnoceanComponentType.LearnTelegram;
	Telegram telegram;
	int typ;
	int func;
	
	/**
	 * Default constructor 
	 * 
	 * Only this constructor is available, a telegram creation like other components
	 * is not necessary.
	 * 
	 * @param telegram Received telegram
	 * @param listener Event subscriber list
	 */
	public LearnTelegram(Telegram telegram, List<?> listeners) {
		super();
		this.telegram = telegram;
		this.typ = telegram.getType();
		this.func = telegram.getFunc();
		this.eventListener = listeners;
		fireFrame();
	}

	/**
	 * Fire the LearnTelegram event
	 */
	void fireFrame() {

		Iterator<?> listen = eventListener.iterator();
		final FrameEventLearnTelegram frameLearnTelegram = new FrameEventLearnTelegram(this, telegram.getDeviceID(), telegram.getFunc(), telegram
				.getType());

		while (listen.hasNext()) {
			((LinkListener) listen.next()).frameReceived_LearnTelegram(frameLearnTelegram);
		}
	}

	@Override
	public String getDeviceID() {
		return telegram.getDeviceID();
	}

	@Override
	public String getORG() {
		return telegram.getxORG();
	}

	@Override
	public EnoceanComponentType getType() {
		return enoceanComponentType;
	}

	@Override
	public void setDeviceID(String deviceID) {
		telegram.setDeviceID(deviceID);
	}

	public EnoceanComponentType getEnoceanComponentType() {
		return enoceanComponentType;
	}

	public void setEnoceanComponentType(EnoceanComponentType enoceanComponentType) {
		this.enoceanComponentType = enoceanComponentType;
	}

	public Telegram getTelegram() {
		return telegram;
	}

	public void setTelegram(Telegram telegram) {
		this.telegram = telegram;
	}

	public int getFunc() {
		return func;
	}

	public void setFunc(int func) {
		this.func = func;
	}

	public int getTyp() {
		return typ;
	}

	public void setTyp(int typ) {
		this.typ = typ;
	}

}
